//
//  CustomNavigationBar.m
//  UIControls
//
//  Created by 21CN Vgo on 11-12-21.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CustomNavigationBar.h"
#import <QuartzCore/QuartzCore.h>


@implementation CustomNavigationBar

@synthesize backgroundImage;

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
	self = [super initWithCoder:aDecoder];
	if (self) {
		// Initialization code.
	}
	return self;
}

- (void)setTintColor:(UIColor *)theTinColor
{
	[super setTintColor:theTinColor];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
	
	if (self.backgroundImage)
	{
		[self.backgroundImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
	}
	else
	{
		[super drawRect:rect];
	}
}

- (void)dealloc {
	self.backgroundImage = nil;
    [super dealloc];
}

- (void)setAlpha:(CGFloat)newAlpha
{
	if (self.translucent && newAlpha > 0.9)
	{
		newAlpha = 0.9;
	}
	[super setAlpha:newAlpha];
}

- (void)setTranslucent:(BOOL)newTranslucent
{
	[super setTranslucent:newTranslucent];
	
	self.alpha = newTranslucent ? 0.9 : 1.0;
}

- (void)setBackgroundImage:(UIImage *)image
{
	SAFE_RELEASE(backgroundImage);
	
	if (image == nil)
	{
		return;
	}
	backgroundImage = [image retain];
	
#ifdef __IPHONE_5_0
	if ([self respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
	{
		[self setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
	}
	else
#endif
	{
		[self setNeedsDisplay];
	}
	
	CATransition *transition = [CATransition animation];
	//transition.duration = 0.75;
	transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];/* 动画的开始与结束的快慢*/
	transition.type = @"fade"; /* 各种动画效果*/
	//@"cube" @"moveIn" @"reveal" @"fade"(default) @"pageCurl" @"pageUnCurl" @"suckEffect" @"rippleEffect" @"oglFlip"
	transition.subtype = kCATransitionFromLeft;   /* 动画方向*/
	[self.layer addAnimation:transition forKey:nil];
}

@end
